﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a sphere in a three dimensional space.
    /// </summary>
    [Serializable]
    public class Sphere : ISphere
    {
        /// <summary>
        /// The centre of the sphere.
        /// </summary>
        private Point3D centre;

        /// <summary>
        /// The radius of the sphere.
        /// </summary>
        private double radius;

        /// <summary>
        /// Initializes a new instance of the <see cref="Sphere"/> class.
        /// </summary>
        /// <param name="radius">The radius of the sphere.</param>
        public Sphere(double radius)
        {
            this.centre = new Point3D();
            this.radius = radius;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sphere"/> class.
        /// </summary>
        /// <param name="sphere">The sphere to clone.</param>
        public Sphere(Sphere sphere)
        {
            if (sphere == (Sphere) null)
            {
                throw new ArgumentNullException("sphere");
            }

            this.centre = sphere.Centre;
            this.radius = sphere.Radius;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Sphere"/> class.
        /// </summary>
        /// <param name="centre">The centre of the sphere.</param>
        /// <param name="radius">The radius of the sphere.</param>
        public Sphere(Point3D centre, double radius)
        {
            if (centre == (Point3D) null)
            {
                throw new ArgumentNullException("centre");
            }

            this.centre = centre;
            this.radius = radius;
        }

        /// <summary>
        /// Gets or sets the centre of the sphere.
        /// </summary>
        /// <value>The centre of the sphere.</value>
        public Point3D Centre
        {
            get { return centre; }
            set { centre = value; }
        }

        /// <summary>
        /// Gets or sets the radius of the sphere.
        /// </summary>
        /// <value>The radius of the sphere.</value>
        public double Radius
        {
            get { return radius; }
            set { radius = value; }
        }

        /// <summary>
        /// Compares the current instance of the sphere to another.
        /// </summary>
        /// <param name="compare">The sphere to compare.</param>
        /// <returns>True if the position and the radius are even to the specified compareshpere 
        /// otherwise, false</returns>
        public bool CompareTo(Sphere compare)
        {
            if (compare == (Sphere) null)
            {
                throw new ArgumentNullException("compare");
            }

            return this == compare;
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first sphere.</param>
        /// <param name="b">The second sphere.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Sphere a, Sphere b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.Centre == b.Centre) && (a.Radius == b.Radius));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first sphere.</param>
        /// <param name="b">The second sphere.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Sphere a, Sphere b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Scales the position elements x, y and z of the sphere with a specified factor.
        /// </summary>
        /// <param name="scaleFactor">The scale factor.</param>
        public void ScaleTransformation(double scaleFactor)
        {
            this.centre.ScaleTransformation(scaleFactor);
        }

        /// <summary>
        /// Scales the position elements x, y and z of the sphere with a specified factor.
        /// </summary>
        /// <param name="scaleFactorX">The scale factor for the x position.</param>
        /// <param name="scaleFactorY">The scale factor for the y position.</param>
        /// <param name="scaleFactorZ">The scale factor for the z position.</param>
        public void ScaleTransformation(double scaleFactorX, double scaleFactorY, double scaleFactorZ)
        {
            this.centre.ScaleTransformation(scaleFactorX, scaleFactorY, scaleFactorZ);
        }

        /// <summary>
        /// Translates the position of the sphere.
        /// </summary>
        /// <param name="translateValue">The translate value for x, y and zs position.</param>
        public void TranslateTransformation(double translateValue)
        {
            this.centre.TranslateTransformation(translateValue);
        }

        /// <summary>
        /// Translates the position of the sphere.
        /// </summary>
        /// <param name="translateValueX">The translate value for x position.</param>
        /// <param name="translateValueY">The translate value for y position.</param>
        /// <param name="translateValueZ">The translate value for z position.</param>
        public void TranslateTransformation(double translateValueX, double translateValueY, double translateValueZ)
        {
            this.centre.TranslateTransformation(translateValueX, translateValueY, translateValueZ);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            if (obj == (object) null)
            {
                throw new ArgumentNullException("obj");
            }

            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return "{ X=" + this.centre.X.ToString() + " Y=" + this.centre.Y.ToString() + " Z=" +
                   this.centre.Z.ToString() + " }";
        }
    }
}